package com.bhs.sansonglogistics.ui.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.LoginBean;
import com.bhs.sansonglogistics.dialog.PrivacyAgreementDialog;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.home.MainActivity;
import com.bhs.sansonglogistics.utils.FastCilckOneUtil;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.PreLoginResultListener;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 一键登录页 废弃
 */
public class OneClickLoginActivity extends BaseActivity implements NetCallBack, XPopupCallback {

    private Button btnOneClickLogin;
    private Button btnChangingOver;
    private TextView tvAgreement;
    private TextView mTvPhoneNumber;
    private android.widget.LinearLayout mLlAgreement;
    private android.widget.ImageView mIvAgreement;


    @Override
    public int initLayout() {
        return R.layout.activity_one_click_login;
    }

    @Override
    public void initView() {
        if (!TextUtils.isEmpty(SharedPreferencesUtils.getString(mContext, "token"))) {
            startActivity(new Intent(OneClickLoginActivity.this, MainActivity.class));
            finish();
            return;
        }
        btnOneClickLogin = findViewById(R.id.btn_one_click_login);
        btnChangingOver = findViewById(R.id.btn_changing_over);
        tvAgreement = findViewById(R.id.tv_agreement);
        mTvPhoneNumber = findViewById(R.id.tv_phone_number);
        mLlAgreement = findViewById(R.id.ll_agreement);
        mIvAgreement = findViewById(R.id.iv_agreement);
    }

    @Override
    public void initData() {
        if (!TextUtils.isEmpty(SharedPreferencesUtils.getString(mContext, "token"))) {
           return;
        }
        btnOneClickLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // startActivity(new Intent(OneClickLoginActivity.this, MainActivity.class));
                login();
            }
        });
        btnChangingOver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OneClickLoginActivity.this, PhoneNumberLoginActivity.class));
            }
        });
        String str = "我已阅读并同意《联通统一认证服务条款》和《用户协议》、《隐私协议》并授权伞送获取本机号码";
        SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(str);
        // 在设置点击事件、同时设置字体颜色
        ClickableSpan clickableSpanOne = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                if (FastCilckOneUtil.isFastClick()) {
                    return;
                }
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };
        ClickableSpan clickableSpanTwo = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                if (FastCilckOneUtil.isFastClick()) {
                    return;
                }
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };

        ClickableSpan clickableSpanThree = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                if (FastCilckOneUtil.isFastClick()) {
                    return;
                }
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };
        spannableBuilder.setSpan(clickableSpanOne, 7, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableBuilder.setSpan(clickableSpanTwo, 20, 26, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableBuilder.setSpan(clickableSpanThree, 27, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvAgreement.setMovementMethod(LinkMovementMethod.getInstance());
        tvAgreement.setText(spannableBuilder);
    }



    private void login() {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("login_type", "1");
        parameters.put("mobile", "13961661754");
        parameters.put("platform", "logistics");
        loadingPopupShow();
        networkRequest(netApi.login(parameters), this);
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        LoginBean loginBean = new Gson().fromJson(json, LoginBean.class);
        if (loginBean.isStatus()) {
            SharedPreferencesUtils.saveString(mContext, "token", loginBean.getData().getUser().getLogin_token());
            loadingPopup.dismissWith(new Runnable() {
                @Override
                public void run() {
                    //if()
                    startActivity(new Intent(OneClickLoginActivity.this, MainActivity.class));
                    finish();
                }
            });

        } else {
            loadingPopupDismiss();
        }

    }

    @Override
    public void onFailure(int requestCode) {
    }

    @Override
    public void onCreated(BasePopupView popupView) {

    }

    @Override
    public void beforeShow(BasePopupView popupView) {

    }

    @Override
    public void onShow(BasePopupView popupView) {

    }

    @Override
    public void onDismiss(BasePopupView popupView) {
        if (SharedPreferencesUtils.getBoolean(mContext, "privacy", false)) {
            mIvAgreement.setImageResource(R.mipmap.ic_checked);
        }
    }

    @Override
    public void beforeDismiss(BasePopupView popupView) {

    }

    @Override
    public boolean onBackPressed(BasePopupView popupView) {
        return false;
    }

    @Override
    public void onKeyBoardStateChanged(BasePopupView popupView, int height) {

    }

    @Override
    public void onDrag(BasePopupView popupView, int value, float percent, boolean upOrLeft) {

    }

    @Override
    public void onClickOutside(BasePopupView popupView) {

    }
}